package util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class XMLHttpUtil {


	public static void main(String[] arg){

			/*String str=new XMLHttpUtil().httpPost("http://172.18.2.70/oaser/services/user/login?user=likz&pwd=000000", "utf-8");
			System.out.println(str);*/
	 
			
			String str=new XMLHttpUtil().httpPost("http://172.18.2.70/oaser/services/user?wsdl", "utf-8");
			System.out.println(str);

	}

	
	/**
	 * 获取Java接口数据
	 * @param url
	 * @param endcoding
	 * @return
	 */
	public static String getService(String url,String endcoding){
		String responseContent=httpPost(url,endcoding);
		return responseContent.substring((responseContent.indexOf("<ns:return>")+11), responseContent.lastIndexOf("</ns:return>"));
	}

	public static String httpPost(String strurl, String encoding)
	  {
		HttpURLConnection http = null;
		String responseContent = null;
		try {
			URL url = new URL(strurl);
			http = (HttpURLConnection) url.openConnection();
			http.setRequestMethod("GET");
			http.setConnectTimeout(10000);
			http.setRequestProperty("Charset", encoding);
			http.setDefaultUseCaches(false);
			http.setDoOutput(true);
			StringBuffer params = new StringBuffer();
			if (params.length() > 0) {
				params = params.deleteCharAt(params.length() - 1);
			}
			http.connect();// 
			InputStream in = http.getInputStream();// 

			BufferedReader rd = new BufferedReader(new InputStreamReader(in,
					encoding));
			String tempLine = rd.readLine();
			StringBuffer tempStr = new StringBuffer();
			String crlf = System.getProperty("line.separator");
			while (tempLine != null) {
				tempStr.append(tempLine);
				tempStr.append(crlf);
				tempLine = rd.readLine();
			}
			responseContent = tempStr.toString().trim();
			rd.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		} finally {
			if (http != null) {
				http.disconnect();
			}
		}
		return responseContent;
	}
}
